//
//  RegisterViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/3/17.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "RegisterViewController.h"
#import "PublicRegisterApi.h"
#import "PublicGetCheckCodeApi.h"
@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *verifyTextField;

@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@property (weak, nonatomic) IBOutlet UIButton *verifyBtn;

@property (weak, nonatomic) IBOutlet UIButton *maskBtn;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationStyle];
    [_phoneTextField setValue:[UIColor colorWithHexString:@"B8B8B8"] forKeyPath:@"_placeholderLabel.textColor"];
    [_pwdTextField setValue:[UIColor colorWithHexString:@"B8B8B8"] forKeyPath:@"_placeholderLabel.textColor"];
    [_verifyTextField setValue:[UIColor colorWithHexString:@"B8B8B8"] forKeyPath:@"_placeholderLabel.textColor"];
}

- (void)setNavigationStyle{
    
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setNavigationTitle:@"注册" titleColor:nil];
    [self hideNavigationBottomLine];
}

- (IBAction)registerActon:(UIButton *)sender {
    
    if (![_phoneTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (![_pwdTextField.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-20位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    if (_verifyTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"亲，还未输入验证码..."];
        return;
    }
    
    if (_verifyTextField.text.length < 6 || _verifyTextField.text.length > 6) {
        [SVProgressHUD showErrorWithStatus:@"亲，请输入6位验证码..."];
        return;
    }
    
    [self registerActionStart];
    
}

- (void)registerActionStart{
    
    
   [[DTCloudManager defaultJNI_iOS_SDK] registerWithUsername:_phoneTextField.text password:_pwdTextField.text securityCode:_verifyTextField.text successCallback:^(NSDictionary *dic) {
       [SVProgressHUD showSuccessWithStatus:@"注册成功"];
       
       [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_REGISTER_SUCCESS
                                                          object:nil
                                                        userInfo:@{@"account":_phoneTextField.text,
                                                                   @"password":_pwdTextField.text}];
       [self.navigationController popViewControllerAnimated:YES];

   } errorCallback:^(NSDictionary *dic) {
       
       [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];

   }];

    
    
}
- (IBAction)verifyAction:(UIButton *)sender {
    
    if (![_phoneTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    [SVProgressHUD showInfoWithStatus:@"正在获取验证码..."];
    
    __weak typeof(self) weakself = self;
    
    [[DTCloudManager defaultJNI_iOS_SDK] getRegisterCodeWithUsername:_phoneTextField.text successCallback:^(NSDictionary *dic) {
        
        [SVProgressHUD showSuccessWithStatus:@"验证码已经发送到手机！"];
        [weakself setVerifyBtnTimer];

    } errorCallback:^(NSDictionary *dic) {
        
        [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
    }];
    

    
}
// 设置定时器
- (void)setVerifyBtnTimer {
    __block int code_out = 60;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (code_out <= 0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_verifyBtn setTitle:@"获取验证码" forState:0];
                [_verifyBtn setTitle:@"60秒后获取" forState:UIControlStateDisabled];
                _verifyBtn.enabled = YES;
                code_out = 60;
            });
        } else {
            _verifyBtn.enabled = NO;
            NSString * time = [NSString stringWithFormat:@"%d秒后获取",code_out];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_verifyBtn setTitle:time forState:UIControlStateDisabled];
            });
            code_out -- ;
        }
    });
    dispatch_resume(timer);
}

- (IBAction)maskAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.pwdTextField.secureTextEntry = !sender.selected;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
